package material

import (
	"flag"
	"fmt"
	"github.com/urfave/cli"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/logc"
	"go-zero-demo/app/datacenter/cron/internal/cmd"
	"go-zero-demo/app/datacenter/cron/internal/config"
	"go-zero-demo/app/datacenter/cron/internal/logic/material"
	"go-zero-demo/app/datacenter/cron/internal/svc"
	"strconv"
	"strings"
)

var MaterialDownloadServ = cli.Command{
	Name:  "material.download",
	Usage: "快手广告主信息",
	Flags: []cli.Flag{
		cmd.StringFlag("config, c", "./etc/cron.yaml", "Custom configuration file path"),
		cmd.BoolFlag("is_today", "是否同步当天数据"),
		cmd.IntFlag("day,d", 1, "最近几天的数据，默认当天"),
		cmd.StringFlag("stime,st", "", "开始时间：eg 2022-07-01"),
		cmd.StringFlag("etime,et", "", "结束时间：eg 2022-07-02"),
		cmd.StringFlag("advertiser_ids,advs", "", "逗号分割要同步的广告主ids"),
		cmd.IntFlag("thread,t", 3, "使用线程数: 3"),
		cmd.IntFlag("platform,p", 1, "平台类型"),
	},
	Action: runMaterialDownload,
}

func runMaterialDownload(cliCtx *cli.Context) {

	flag.Parse()

	var c config.Config
	conf.MustLoad(cliCtx.String("config"), &c)
	logc.MustSetup(c.Log)

	advs := cliCtx.String("advertiser_ids")
	var advIds []uint64
	if advs != "" {
		advsStr := strings.Split(advs, ",")
		for _, v := range advsStr {
			i, err := strconv.ParseUint(v, 10, 64)
			if err != nil {
				fmt.Println("广告主错误", err)
				return
			}
			advIds = append(advIds, i)
		}
	}

	ctx := svc.NewDownloadSvc(c)
	s := material.NewServer(ctx)
	s.Run(cliCtx.Int("day"), cliCtx.String("stime"), cliCtx.String("etime"),
		cliCtx.Bool("is_today"), cliCtx.Int("thread"), cliCtx.Int("platform"))
}
